﻿namespace PlugNT.Database.Common
{
    using PlugNT.Database;
    using PlugNT.Database.Common.Simple;
    using PlugNT.Database.Common.Complex;
    using PlugNT.Safe;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;


    public class Tables
    {
        public static bool Add(string tableName, Dictionary<string, object> dictions)
        {
            return Add(GetDbInfo(), tableName, dictions);
        }
        public static bool Add(DbInfo dbinfo, string tableName, Dictionary<string, object> dictions)
        {
            bool flag = false;
            bool flag2 = true;
            StringBuilder builder = new StringBuilder();
            StringBuilder builder2 = new StringBuilder();
            builder.Append("insert into " + GetTableName(tableName) + " (");
            foreach (KeyValuePair<string, object> pair in dictions)
            {
                if (flag2)
                {
                    builder.Append("[" + pair.Key + "]");
                    builder2.Append(ConvertToString(pair.Value));
                    flag2 = false;
                }
                else
                {
                    builder.Append(",[" + pair.Key + "]");
                    builder2.Append("," + ConvertToString(pair.Value));
                }
            }
            builder.Append(")");
            builder.Append(" values (");
            builder.Append(builder2.ToString());
            builder.Append(")");
            try
            {
                if (Complex.DbHelper.ExecuteSql(dbinfo, builder.ToString()) > 0)
                {
                    flag = true;
                }
            }
            catch
            {
            }
            return flag;
        }

        public static bool Add(DbInfo dbinfo, string tableName, Hashtable hashtab)
        {
            bool flag = false;
            bool flag2 = true;
            StringBuilder builder = new StringBuilder();
            StringBuilder builder2 = new StringBuilder();
            builder.Append("insert into " + GetTableName(tableName) + " (");
            foreach (DictionaryEntry entry in hashtab)
            {
                if (flag2)
                {
                    builder.Append("[" + entry.Key.ToString() + "]");
                    builder2.Append(ConvertToString(entry.Value));
                    flag2 = false;
                }
                else
                {
                    builder.Append(",[" + entry.Key.ToString() + "]");
                    builder2.Append("," + ConvertToString(entry.Value));
                }
            }
            builder.Append(")");
            builder.Append(" values (");
            builder.Append(builder2.ToString());
            builder.Append(")");
            try
            {
                if (Complex.DbHelper.ExecuteSql(dbinfo, builder.ToString()) > 0)
                {
                    flag = true;
                }
            }
            catch
            {
            }
            return flag;
        }

        public static object ConvertToObject(Type type, string str)
        {
            switch (type.ToString())
            {
                case "System.Boolean":
                    return Convert.ToBoolean(str);

                case "System.Decimal":
                    return Convert.ToDecimal(str);

                case "System.Double":
                    return Convert.ToDouble(str);

                case "System.Single":
                    return Convert.ToSingle(str);

                case "System.Byte":
                    return Convert.ToByte(str);

                case "System.SByte":
                    return Convert.ToSByte(str);

                case "System.Int16":
                    return Convert.ToInt16(str);

                case "System.Int32":
                    return Convert.ToInt32(str);

                case "System.Int64":
                    return Convert.ToInt64(str);

                case "System.DateTime":
                    return Convert.ToDateTime(str);

                case "System.Char":
                    return Convert.ToChar(str);
            }
            return str;
        }

        private static string ConvertToString(object obj)
        {
            switch (obj.GetType().ToString())
            {
                case "System.Boolean":
                case "System.Decimal":
                case "System.Double":
                case "System.Single":
                case "System.Byte":
                case "System.SByte":
                case "System.Int16":
                case "System.Int32":
                case "System.Int64":
                    return obj.ToString();

                case "System.DateTime":
                    return ("'" + obj.ToString() + "'");

                case "System.Char":
                case "System.String":
                    return ("'" + StringHelper.SqlFilter(obj.ToString()) + "'");
            }
            return ("'" + StringHelper.SqlFilter(obj.ToString()) + "'");
        }

        public static int Delete(string tableName, string andWhere)
        {
            return Delete(GetDbInfo(), tableName, andWhere);
        }

        public static int Delete(DbInfo dbinfo, string tableName, string andWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from [" + GetTableName(tableName) + "] ");
            builder.Append(" where 1=1 " + andWhere);
            return Complex.DbHelper.ExecuteSql(dbinfo, builder.ToString());
        }

        public static int GetCount(string tableName, string andWhere)
        {
            return GetCount(GetDbInfo(), tableName, andWhere);
        }

        public static int GetCount(DbInfo dbinfo, string tableName, string andWhere)
        {
            string sQLString = "select count(1) from [" + GetTableName(tableName) + "] where 1=1 " + andWhere;
            return (int)Complex.DbHelper.GetSingle(dbinfo, sQLString);
        }

        private static DbInfo GetDbInfo()
        {
            return Simple.DbHelper.dbinfo;
        }

        public static DataTable GetList(string tableName, string andWhere)
        {
            return GetList(GetDbInfo(), tableName, andWhere);
        }

        public static DataTable GetList(DbInfo dbinfo, string tableName, string andWhere)
        {
            return Complex.DbHelper.TabQuery(dbinfo, string.Format("select * from [" + GetTableName(tableName) + "] where 1=1 {0}", andWhere));
        }

        public static DataTable GetList(string tableName, int topCount, string andWhere)
        {
            return GetList(GetDbInfo(), tableName, topCount, andWhere);
        }

        public static DataTable GetList(DbInfo dbinfo, string tableName, int topCount, string andWhere)
        {
            string str = (topCount <= 0) ? "" : (" top " + topCount.ToString());
            return Complex.DbHelper.TabQuery(dbinfo, string.Format("select {0} * from [" + GetTableName(tableName) + "] where 1=1 {1}", str, andWhere));
        }

        public static DataTable GetList(string tableName, int pageSize, int currPage, string andWhere)
        {
            return GetList(GetDbInfo(), tableName, pageSize, currPage, andWhere);
        }

        public static DataTable GetList(DbInfo dbinfo, string tableName, int pageSize, int currPage, string andWhere)
        {
            string sQLString = Sql.GetPagingString(pageSize, currPage, GetTableName(tableName), "id", andWhere);
            return Complex.DbHelper.TabQuery(dbinfo, sQLString);
        }

        public static string GetObjectString(string tableName, string columnName, string andWhere)
        {
            return GetObjectString(GetDbInfo(), tableName, columnName, andWhere);
        }

        public static string GetObjectString(DbInfo dbinfo, string tableName, string columnName, string andWhere)
        {
            object single = Complex.DbHelper.GetSingle(dbinfo, string.Format("select top 1 [{0}] from [" + GetTableName(tableName) + "] where 1=1 {1}", columnName, andWhere));
            return ((single == null) ? "" : single.ToString());
        }

        public static string GetReSetTableStr(string tabName, string idName)
        {
            return GetReSetTableStr(GetDbInfo(), tabName, idName);
        }

        public static string GetReSetTableStr(DbInfo dbinfo, string tabName, string idName)
        {
            switch (dbinfo.DbType)
            {
                case "System.Data.SqlClient":
                    return string.Format("dbcc checkident({0},reseed,1)", StringHelper.SqlFilter(tabName));

                case "System.Data.OleDb":
                    return string.Format("alter table [{0}] alter column {1} counter(1,1)", StringHelper.SqlFilter(tabName), StringHelper.SqlFilter(idName));

                case "System.Data.OracleClient":
                    return string.Format("dbcc checkident({0},reseed,1)", StringHelper.SqlFilter(tabName));
            }
            return "";
        }

        private static string GetTableName(string tableName)
        {
            return StringHelper.SqlFilter(tableName);
        }

        public static bool Update(string tableName, Dictionary<string, object> dictions, string andWhere)
        {
            return Update(GetDbInfo(), tableName, dictions, andWhere);
        }

        public static bool Update(DbInfo dbinfo, string tableName, Dictionary<string, object> dictions, string andWhere)
        {
            bool flag = false;
            bool flag2 = true;
            StringBuilder builder = new StringBuilder();
            builder.Append("update " + GetTableName(tableName) + " set ");
            foreach (KeyValuePair<string, object> pair in dictions)
            {
                if (flag2)
                {
                    builder.Append("[" + pair.Key + "]=");
                    flag2 = false;
                }
                else
                {
                    builder.Append(",[" + pair.Key + "]=");
                }
                builder.Append(ConvertToString(pair.Value));
            }
            builder.Append(" where 1=1 " + andWhere);
            try
            {
                if (Complex.DbHelper.ExecuteSql(dbinfo, builder.ToString()) > 0)
                {
                    flag = true;
                }
            }
            catch
            {
            }
            return flag;
        }

        public static bool Update(DbInfo dbinfo, string tableName, Hashtable hashtab, string andWhere)
        {
            bool flag = false;
            bool flag2 = true;
            StringBuilder builder = new StringBuilder();
            builder.Append("update " + GetTableName(tableName) + " set ");
            foreach (DictionaryEntry entry in hashtab)
            {
                if (flag2)
                {
                    builder.Append("[" + entry.Key.ToString() + "]=");
                    flag2 = false;
                }
                else
                {
                    builder.Append(",[" + entry.Key.ToString() + "]=");
                }
                builder.Append(ConvertToString(entry.Value));
            }
            builder.Append(" where 1=1 " + andWhere);
            try
            {
                if (Complex.DbHelper.ExecuteSql(dbinfo, builder.ToString()) > 0)
                {
                    flag = true;
                }
            }
            catch
            {
            }
            return flag;
        }
    }
}

